twenty20batting2007 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2007twenty20battingrating.csv")
twenty20batting2008 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2008twenty20battingrating.csv")
twenty20batting2009 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2009twenty20battingrating.csv")
twenty20batting2010 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2010twenty20battingrating.csv")
twenty20batting2011 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2011twenty20battingrating.csv")
twenty20batting2012 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2012twenty20battingrating.csv")
twenty20batting2013 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2013twenty20battingrating.csv")
twenty20batting2014 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2014twenty20battingrating.csv")
twenty20batting2015 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2015twenty20battingrating.csv")
twenty20batting2016 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2016twenty20battingrating.csv")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dataTwenty20Batting <- bind_rows(twenty20batting2007, twenty20batting2008, twenty20batting2009, twenty20batting2010,
                             twenty20batting2011, twenty20batting2012, twenty20batting2013, twenty20batting2014,
                             twenty20batting2015, twenty20batting2016)
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
summary(dataTwenty20Batting)
##      Name               Rating     
##  Length:1000        Min.   : 10.0  
##  Class :character   1st Qu.:201.0  
##  Mode  :character   Median :316.5  
##                     Mean   :349.1  
##                     3rd Qu.:484.0  
##                     Max.   :897.0
library(VIM)
## Loading required package: colorspace
## Loading required package: grid
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## VIM is ready to use. 
##  Since version 4.0.0 the GUI is in its own package VIMGUI.
## 
##           Please use the package to use the new (and old) GUI.
## Suggestions and bug-reports can be submitted at: https://github.com/alexkowa/VIM/issues
## 
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
## 
##     sleep
aggr(dataTwenty20Batting)

dataTwenty20Batting <- dataTwenty20Batting %>%
  group_by(Name) %>%
  summarise(avg = mean(Rating))

set.seed(20)

batcluster <- kmeans(dataTwenty20Batting[, 2], 5)

batcluster$cluster <- as.factor(batcluster$cluster)
library(ggplot2)

library(DT)

ggplot(dataTwenty20Batting, aes(dataTwenty20Batting$Name, avg, color = batcluster$cluster)) +
  geom_point(size = 2) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  ggtitle("Twenty20 Batting Ratings(2007-2016)")

dat <- arrange(dataTwenty20Batting, desc(avg)) %>%
  mutate(rank = 1:nrow(dataTwenty20Batting))

dataTwenty20Batting <- merge(dataTwenty20Batting, dat, by  = "Name") 
dataTwenty20Batting
##                   Name    avg.x    avg.y rank
## 1         Abdul Razzaq  59.0000  59.0000  254
## 2          Alok Kapali  72.0000  72.0000  250
## 3          Farhad Reza  78.0000  78.0000  249
## 4         Kamran Akmal 117.0000 117.0000  235
## 5     Mashrafe Mortaza 118.0000 118.0000  234
## 6       Naved ul Hasan  83.0000  83.0000  247
## 7          Salman Butt 114.0000 114.0000  236
## 8           A A Obanda 158.8000 158.8000  216
## 9      A B de Villiers 421.7000 421.7000   56
## 10             A Bagai 287.7500 287.7500  125
## 11           A C Botha 151.0000 151.0000  219
## 12       A C Gilchrist 219.0000 219.0000  178
## 13      A C L Richards  13.0000  13.0000  280
## 14           A D Hales 738.4000 738.4000    1
## 15     A D Mascarenhas 111.5000 111.5000  238
## 16         A D Mathews 388.5000 388.5000   70
## 17         A D Poynter 242.0000 242.0000  160
## 18      A D S Fletcher 262.7500 262.7500  146
## 19          A Flintoff  32.0000  32.0000  265
## 20           A J Finch 594.8333 594.8333   14
## 21         A J Redmond 253.0000 253.0000  153
## 22         A J Strauss 174.0000 174.0000  203
## 23          A M Rahane 289.7500 289.7500  124
## 24           A M Samad 159.0000 159.0000  214
## 25            A N Cook  44.0000  44.0000  259
## 26        A N Kervezee 282.0000 282.0000  131
## 27               A Nel  16.0000  16.0000  274
## 28         A P Devcich 228.0000 228.0000  171
## 29          A R Cusack 173.6667 173.6667  204
## 30           A R White 171.0000 171.0000  205
## 31           A Symonds 533.5000 533.5000   22
## 32        Abdul Razzaq 254.6000 254.6000  152
## 33        Abdur Rehman  13.0000  13.0000  281
## 34         Adnan Ilyas 343.0000 343.0000   89
## 35         Aftab Ahmed 277.3333 277.3333  137
## 36       Ahmed Shehzad 452.0000 452.0000   42
## 37        Anamul Haque 430.0000 430.0000   52
## 38    Asghar Stanikzai 291.0000 291.0000  122
## 39        B B McCullum 662.0000 662.0000    5
## 40          B J Haddin 250.1667 250.1667  156
## 41           B J Hodge 165.0000 165.0000  210
## 42               B Lee 159.3333 159.3333  213
## 43      B M A J Mendis 281.0000 281.0000  134
## 44          B N Cooper 271.0000 271.0000  140
## 45        B R M Taylor 364.4286 364.4286   84
## 46         Babar Hayat 453.0000 453.0000   41
## 47          C A Ingram 311.0000 311.0000  110
## 48        C D Barnwell 226.0000 226.0000  173
## 49        C D McMillan  72.0000  72.0000  251
## 50           C H Gayle 657.8000 657.8000    7
## 51        C J Anderson 261.0000 261.0000  149
## 52       C J Chibhabha 304.2222 304.2222  113
## 53        C K Coventry 179.0000 179.0000  199
## 54      C K Kapugedera 223.5000 223.5000  176
## 55      C K Langeveldt  19.0000  19.0000  269
## 56        C Kieswetter 473.6667 473.6667   32
## 57           C L White 434.3750 434.3750   49
## 58             C Munro 324.0000 324.0000   95
## 59           C O Obuya 209.2000 209.2000  182
## 60      C R D Fernando  79.5000  79.5000  248
## 61          C R Ervine 199.6667 199.6667  188
## 62         C S MacLeod 265.5000 265.5000  145
## 63          D A Miller 365.8333 365.8333   83
## 64          D A Warner 660.2500 660.2500    6
## 65           D F Watts 128.0000 128.0000  229
## 66         D J G Sammy 204.0000 204.0000  184
## 67          D J Hussey 591.2500 591.2500   15
## 68         D J J Bravo 380.2000 380.2000   75
## 69         D L Vettori 129.0000 129.0000  228
## 70           D M Bravo 270.7500 270.7500  142
## 71           D O Obuya 210.6667 210.6667  181
## 72   D P M Jayawardene 526.4286 526.4286   25
## 73           D R Flynn 124.0000 124.0000  231
## 74           D R Smith 315.5714 315.5714  108
## 75          D R Tuffey  50.0000  50.0000  256
## 76            D Ramdin 169.3333 169.3333  207
## 77           D S Smith 322.3333 322.3333   99
## 78           E C Joyce 299.0000 299.0000  114
## 79        E Chigumbura 356.2500 356.2500   86
## 80        E J G Morgan 679.8571 679.8571    2
## 81    E S Szwarczynski 251.4000 251.4000  154
## 82        F du Plessis 665.7500 665.7500    4
## 83         Farhad Reza  33.0000  33.0000  264
## 84          Fawad Alam 127.5000 127.5000  230
## 85            G B Hogg 147.0000 147.0000  222
## 86           G C Smith 649.0000 649.0000    8
## 87          G C Wilson 281.5714 281.5714  133
## 88       G E F Barnett 163.0000 163.0000  211
## 89           G Gambhir 641.4000 641.4000    9
## 90          G J Bailey 386.0000 386.0000   71
## 91         G J Maxwell 470.0000 470.0000   35
## 92             G Malla 215.0000 215.0000  180
## 93       Gulbadin Naib 276.7500 276.7500  138
## 94    H D R Thirimanne 285.0000 285.0000  128
## 95      H D Rutherford 385.0000 385.0000   72
## 96            H Davids 468.0000 468.0000   36
## 97           H H Gibbs 319.6667 319.6667  104
## 98            H M Amla 528.0000 528.0000   24
## 99         H Masakadza 521.5000 521.5000   29
## 100            H Patel 188.3333 188.3333  194
## 101        I J L Trott  17.0000  17.0000  271
## 102         I K Pathan 144.6667 144.6667  223
## 103           I R Bell 241.6667 241.6667  161
## 104        Imran Nazir 262.3333 262.3333  148
## 105         J A Morkel 349.0000 349.0000   87
## 106            J Botha 160.0000 160.0000  212
## 107        J C Buttler 439.2500 439.2500   46
## 108          J Charles 407.0000 407.0000   63
## 109         J D P Oram 323.1667 323.1667   96
## 110          J D Ryder 295.1667 295.1667  118
## 111     J E C Franklin 199.3333 199.3333  189
## 112           J E Root 463.6667 463.6667   38
## 113         J F Mooney 153.6667 153.6667  218
## 114         J H Kallis 473.0000 473.0000   33
## 115            J J Roy 425.0000 425.0000   53
## 116   J J van der Wath 124.0000 124.0000  232
## 117         J L Ontong 189.0000 189.0000  193
## 118       J M Anderson  14.0000  14.0000  276
## 119       J M Bairstow 238.0000 238.0000  163
## 120           J M Kemp 103.0000 103.0000  241
## 121          J M Vince 366.0000 366.0000   82
## 122          J Mubarak 279.0000 279.0000  135
## 123         J P Duminy 564.3333 564.3333   19
## 124          J R Hopes  48.0000  48.0000  257
## 125    Junaid Siddique 241.0000 241.0000  162
## 126        K A Pollard 323.0000 323.0000   97
## 127     K C Sangakkara 494.4286 494.4286   31
## 128          K D Mills 123.3333 123.3333  233
## 129        K J Coetzer 416.0000 416.0000   57
## 130        K J O'Brien 227.7500 227.7500  172
## 131      K K D Karthik  94.5000  94.5000  243
## 132      K P Pietersen 674.2857 674.2857    3
## 133     K S Williamson 434.0000 434.0000   50
## 134       Kamran Akmal 406.0000 406.0000   65
## 135        Karim Sadiq 224.0000 224.0000  175
## 136      L D Chandimal 317.0000 317.0000  107
## 137         L E Bosman 295.0000 295.0000  119
## 138         L J Wright 320.3333 320.3333  102
## 139      L M P Simmons 394.2222 394.2222   68
## 140        L P C Silva 110.3333 110.3333  239
## 141         L R Taylor 410.4000 410.4000   61
## 142           L Ronchi 342.0000 342.0000   90
## 143          L Vincent 203.0000 203.0000  185
## 144          M A Leask 184.0000 184.0000  196
## 145     M D K J Perera 577.5000 577.5000   17
## 146       M E K Hussey 357.6667 357.6667   85
## 147       M F Maharoof  20.0000  20.0000  268
## 148          M H Cross 279.0000 279.0000  136
## 149          M H Yardy 132.5000 132.5000  226
## 150         M J Clarke 232.7500 232.7500  168
## 151        M J Guptill 582.0000 582.0000   16
## 152           M J Lumb 287.0000 287.0000  126
## 153          M J Prior 221.6667 221.6667  177
## 154          M J Vijay 168.0000 168.0000  208
## 155         M L Hayden 154.0000 154.0000  217
## 156       M L Udawatte 178.0000 178.0000  200
## 157        M N Samuels 414.9000 414.9000   58
## 158        M N van Wyk 321.7500 321.7500  101
## 159         M N Waller 318.7500 318.7500  106
## 160            M Ntini  24.0000  24.0000  267
## 161      M R Gillespie  19.0000  19.0000  270
## 162          M R Swart 534.7500 534.7500   21
## 163        M S Chapman 384.0000 384.0000   73
## 164          M S Dhoni 439.3333 439.3333   45
## 165       M S Sinclair  14.0000  14.0000  277
## 166           M S Wade 248.3333 248.3333  158
## 167        M V Boucher 207.0000 207.0000  183
## 168         M W Machan 411.7500 411.7500   60
## 169        Mahmudullah 290.0000 290.0000  123
## 170      Mandeep Singh 272.0000 272.0000  139
## 171   Mashrafe Mortaza 174.3333 174.3333  202
## 172     Mirwais Ashraf 135.0000 135.0000  225
## 173      Misbah ul Haq 577.2500 577.2500   18
## 174  Mohammad Ashraful 325.8000 325.8000   94
## 175    Mohammad Hafeez 406.1000 406.1000   64
## 176      Mohammad Nabi 293.3333 293.3333  120
## 177   Mohammad Shahzad 450.4286 450.4286   43
## 178     Mohammad Usman 323.0000 323.0000   98
## 179    Mohammad Yousuf  42.0000  42.0000  262
## 180      Mukhtar Ahmed 434.5000 434.5000   48
## 181    Mushfiqur Rahim 369.5000 369.5000   80
## 182        N J O'Brien 250.2857 250.2857  155
## 183       N L McCullum 187.0000 187.0000  195
## 184       N L T Perera 320.0000 320.0000  103
## 185          N R Kumar 190.0000 190.0000  192
## 186         N S Poonia 171.0000 171.0000  206
## 187        N W Bracken  17.0000  17.0000  272
## 188    Nadif Chowdhury  32.0000  32.0000  266
## 189        Naeem Islam 180.0000 180.0000  198
## 190  Najibullah Zadran 284.6667 284.6667  130
## 191      Nasir Hossain 297.7500 297.7500  116
## 192      Nasir Jamshed 424.0000 424.0000   54
## 193     Naved ul Hasan 102.0000 102.0000  242
## 194    Noor Ali Zadran 311.0000 311.0000  111
## 195      Nowroz Mangal 201.0000 201.0000  186
## 196           O A Shah 396.0000 396.0000   67
## 197    P D Collingwood 467.0000 467.0000   37
## 198      P D McGlashan  13.0000  13.0000  282
## 199         P G Fulton 114.0000 114.0000  237
## 200        P J Ongondo  63.0000  63.0000  253
## 201        P L Mommsen 286.5000 286.5000  127
## 202       P R Stirling 470.2000 470.2000   34
## 203           P Utseya  43.0000  43.0000  261
## 204         P W Borren 268.5000 268.5000  143
## 205       Paras Khadka 232.0000 232.0000  169
## 206          Q de Kock 532.6667 532.6667   23
## 207         R A Jadeja 139.0000 139.0000  224
## 208     R D Berrington 381.8000 381.8000   74
## 209           R E Levi 522.0000 522.0000   28
## 210         R G Sharma 459.2222 459.2222   39
## 211       R Gunasekera 235.0000 235.0000  166
## 212          R J Nicol 307.5000 307.5000  112
## 213       R J Peterson  14.0000  14.0000  278
## 214       R M L Taylor 196.0000 196.0000  190
## 215  R N ten Doeschate 233.0000 233.0000  167
## 216      R R Hendricks 296.0000 296.0000  117
## 217          R R Patel 292.0000 292.0000  121
## 218        R R Rossouw 285.0000 285.0000  129
## 219         R R Sarwan 327.0000 327.0000   93
## 220         R R Watson 236.0000 236.0000  164
## 221          R Rampaul  10.0000  10.0000  284
## 222         R S Bopara 312.6667 312.6667  109
## 223         R S Morton  90.5000  90.5000  244
## 224        R T Ponting 523.5000 523.5000   27
## 225        R V Uthappa 215.8000 215.8000  179
## 226      Rizwan Cheema 346.2000 346.2000   88
## 227         S B Styris 375.7500 375.7500   76
## 228        S C J Broad  14.0000  14.0000  279
## 229       S C Williams 224.2000 224.2000  174
## 230      S Chanderpaul 254.6667 254.6667  151
## 231           S Dhawan 322.0000 322.0000  100
## 232           S E Bond  36.0000  36.0000  263
## 233          S E Marsh 166.7500 166.7500  209
## 234        S I Mahmood  11.0000  11.0000  283
## 235        S J Myburgh 438.2500 438.2500   47
## 236          S K Raina 551.1111 551.1111   20
## 237        S L Malinga 159.0000 159.0000  215
## 238        S M Pollock  90.0000  90.0000  245
## 239     S Matsikenyeri  44.0000  44.0000  260
## 240         S O Tikolo 281.8000 281.8000  132
## 241        S P D Smith 259.5000 259.5000  150
## 242       S P Khakurel 200.0000 200.0000  187
## 243         S R Watson 519.0000 519.0000   30
## 244     S T Jayasuriya 526.2000 526.2000   26
## 245      Sabbir Rahman 432.5000 432.5000   51
## 246        Salman Butt 441.0000 441.0000   44
## 247 Samiullah Shenwari 271.0000 271.0000  141
## 248      Sarfraz Ahmed 262.5000 262.5000  147
## 249        Shafiqullah 176.0000 176.0000  201
## 250   Shahadat Hossain  16.0000  16.0000  275
## 251      Shahid Afridi 423.4000 423.4000   55
## 252    Shahzaib Hasan  150.0000 150.0000  220
## 253      Shaiman Anwar 394.0000 394.0000   69
## 254    Shakib Al Hasan 339.4000 339.4000   91
## 255    Sharad Vesawkar 250.0000 250.0000  157
## 256      Sharjeel Khan 370.6667 370.6667   77
## 257       Shoaib Malik 404.9000 404.9000   66
## 258     Sohaib Maqsood 192.0000 192.0000  191
## 259      Soumya Sarkar 319.0000 319.0000  105
## 260        T G Southee  55.0000  55.0000  255
## 261       T L W Cooper 370.2000 370.2000   78
## 262        T M Dilshan 612.4444 612.4444   12
## 263          T M Odoyo  66.0000  66.0000  252
## 264           T Mishra 181.6667 181.6667  197
## 265      T N de Grooth 229.5000 229.5000  170
## 266        T T Bresnan  17.0000  17.0000  273
## 267            T Taibu 235.6667 235.6667  165
## 268        Tamim Iqbal 327.8889 327.8889   92
## 269        U T Khawaja 458.0000 458.0000   40
## 270         Umar Akmal 605.8571 605.8571   13
## 271            V Kohli 622.5000 622.5000   11
## 272        V S Solanki  89.0000  89.0000  246
## 273           V Sehwag 298.5000 298.5000  115
## 274          V Sibanda 244.2000 244.2000  159
## 275          W Barresi 414.6667 414.6667   59
## 276  W T S Porterfield 367.2500 367.2500   81
## 277       W U Tharanga 130.3333 130.3333  227
## 278          W W Hinds  45.0000  45.0000  258
## 279       X M Marshall 109.0000 109.0000  240
## 280         Y K Pathan 266.3333 266.3333  144
## 281       Yasir Arafat 150.0000 150.0000  221
## 282        Younis Khan 407.3333 407.3333   62
## 283       Yuvraj Singh 623.3333 623.3333   10
## 284    Zeeshan Maqsood 370.0000 370.0000   79
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(dataTwenty20Batting, x = ~Name, y = ~avg.x, type = 'scatter', 
             mode = 'markers', color = batcluster$cluster, 
             text = ~paste('Rank: ', rank))

p